<?php
// $Id$

/**
 * Controller_Order 控制器
 */
class Controller_Order extends Controller_Abstract
{

	function actionIndex()
	{
        // 为 $this->_view 指定的值将会传递数据到视图中
		# $this->_view['text'] = 'Hello!';
	}
  //结帐键触发操作
  //初始化订单入口
  function actionSetOrder()
  {
    if($this->_app->currentUser())
    {
      if(empty($_SESSION["cart"])
      {
        echo "emptyCart();";
      }
      else
      {
         $this->initialOrder();
         $this->actionShowOrder();
      }
    }
    else
    {
      echo "Login()";
    }
     
  }
 
 //确认并保存订单 
  function actionConfirmOrder()
  {
    $user = $this->_app->currentUserObject();    
    $address_id = $this->_context->address_id;
    $paytool_id = $this->_context->paytool_id;
    $orderList = $user->createOrder();
    if(empty($_SESSION['order']))
    {
      echo "emptyOrder();";
    }
    else
    {
      $order = $_SESSION['order'];
      $orderList->id = $order['id'];
      $orderList->order_time = $order['initime'];
      $orderList->market_price = $order['market_price_sum'];
      $orderList->mall_price =$order['mall_price_sum'];
      $orderList->exp = $order['exp_sum'];
      $orderList->order_status = 0;
      $orderList->address_id = $address_id;
      $orderList->paytool_id = $paytool_id;
      $orderList->save();
      foreach($item as $order['item'])
      {
        $orderListItem = $orderList->createItem($item);
        $orderListItem->save();
      }
    }  
  }
  //在未确定订单前，显示订单内容, 地址信息，付账选项，以便用户修改
  function actionShowOrder()
  {
    if(empty($_SESSION['order']))
    {
      echo "emptyOrder();";
    }
    else
    {
      $order = $_SESSION['order'];
      $user =  $this->_app->currentUserObject();
      $address = Address::find('user_id = ?', $this->_app->currentUserObject()->id())->getAll();
      $paytool = Paytool:find()->getAll();
			$json = json_encode(array($user, $order, $address, $paytool));
			echo "showOrder(".$json.");";
    }  
  }
  
  //根据订单中商品的列表计算总价格
  // $Type 表示价格的类型，包括：market_price，mall_price, exp等                                                       
  function caculatePrice(array $Item, $Type)
  {
    $sum = 0;
    
    foreach($item as $Item)
    {
       $sum = $sum + $item[$Type]*$item['amount'];
    }
    return $sum;
  }
  
  //根据购物车内容初始化订单
  function initialOrder()
  {
    $cart = $_SESSION['cart'];
    $user = $this->_app->currentUserObject();
    $order = array();
    $order['id'] = date(YmdHis).$cart['id'];
    $order['item'] = array();
    $order['initime'] = date("Y-m-d H:i:s");
    
    foreach($item as $cart['items'])
    {
      $orderItem =  array();
      $orderItem['amount'] = $item['amount'];
      $orderItem['goods_onshelf_id'] = $item['goods']['id'];
      $orderItem['name'] = $item['goods']['goodsname'];
      $orderItem['market_price'] = $item['goods']['market_price'];
      $orderItem['marll_price'] = $item['goods']['mall_price'];
      $orderItem['exp'] = $item['goods']['exp'];
      $order['item'] = array_merge($order['item'], array($orderItem));
      
    }
    $order['market_price_sum'] = $this->caculatePrice($order['item'], 'market_price');
    $order['mall_price_sum'] = $this->caculatePrice($order['item'], 'mall_price');
    $order['exp_sum'] = $this->caculate($order['item'], 'exp');
    $_SESSION['order'] = $order;
  }
  
  //展示订单列表
  function actionCheckOrder()
  {
     $orderList = Order::find('user_id = ?', $this->_app->currentUserObject()->id())->getAll();
		 $json = json_encode($order);
		 echo "showOrderList(".$json.");";
  }
  
  //展示订单的详情
  function actionCheckOrderProperties()
  {
    $orderId = $this->_context->orderId;
    $order = Order::find('order_id = ? AND [owner.user_id] = ?',
		$this->_context->order_id,
		$this->_app->currentUserObject()->id())->query();
    $orderItemList = Orderitem::find('order_id=?', $order->order_id)->getAll();
    $order['itemList'] = $orderItemList;
    $json = json_encode($order);
    echo "showOrderProperties(".$json.");";
    
  }
  
}


